//
//  PPUnreadMessageSummary.m
//  pickpic
//
//  Created by Jinxiao on 10/7/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPUnreadMessageSummaryObject.h"

@implementation PPUnreadMessageSummaryObject

- (NSString *)iconURLString {
    if([self.iconKey length] == 0 || [self.iconHost length] == 0) {
        return nil;
    }

    if([self.iconKey hasPrefix:@"http://"]) {
        return self.iconKey;
    } else if([self.iconKey hasPrefix:@"http:/"]) {
        return [self.iconKey stringByReplacingOccurrencesOfString:@"http:/" withString:@"http://"];
    } else if([self.iconKey hasPrefix:@"http:"]) {
        return [self.iconKey stringByReplacingOccurrencesOfString:@"http:" withString:@"http://"];
    }

    return [NSString stringWithFormat:@"%@%@", self.iconHost, self.iconKey];
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeInteger:self.type forKey:@"type"];
    [encoder encodeObject:self.count forKey:@"count"];
    [encoder encodeObject:self.iconKey forKey:@"iconKey"];
    [encoder encodeObject:self.iconHost forKey:@"iconHost"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    self = [super init];

    if(self) {
        self.type = [decoder decodeIntegerForKey:@"type"];
        self.count = [decoder decodeObjectForKey:@"count"];
        self.iconKey = [decoder decodeObjectForKey:@"iconKey"];
        self.iconHost = [decoder decodeObjectForKey:@"iconHost"];
    }

    return self;
}

+ (instancetype)summaryForParameters:(NSDictionary *)parameters {
    PPUnreadMessageSummaryObject *summary = [[PPUnreadMessageSummaryObject alloc] init];

    summary.count = [parameters validNumberForKey:@"count"];
    summary.iconKey = [parameters validStringForKey:@"icon"];
    summary.iconHost = [parameters validStringForKey:@"host"];

    NSString *type = [parameters validStringForKey:@"topType"];
    if([type isEqualToString:@"MyNotify"]) {
        summary.type = PPUnreadMessageSummaryTypeMyNotify;
    } else if([type isEqualToString:@"Friend"]) {
        summary.type = PPUnreadMessageSummaryTypeFriend;
    } else if([type isEqualToString:@"Follow"]) {
        summary.type = PPUnreadMessageSummaryTypeMiao;
    } else if([type isEqualToString:@"Discovery"]) {
        summary.type = PPUnreadMessageSummaryTypeFound;
    } else if([type isEqualToString:@"MyFavorite"]) {
        summary.type = PPUnreadMessageSummaryTypeFavorite;
    }

    return summary;
}

@end
